import request from '@/api/request'
import type { AxiosProgressEvent } from 'axios'

// 分片上传单片接口（咨询师简历）
export function uploadFileChunkApi(
  params: {
    module: string
    fileId: string
    chunkIndex: number
    chunk: Blob
  },
  onProgress?: (evt: AxiosProgressEvent) => void,
) {
  const { fileId, chunkIndex, chunk, module } = params
  if (!fileId) throw new Error('fileId 不能为空')
  const fd = new FormData()
  fd.append('module', module)
  fd.append('fileId', fileId)
  fd.append('chunkIndex', String(chunkIndex))
  fd.append('chunk', chunk, `part-${chunkIndex}`)
  // console.log('params:', { module, fileId, chunkIndex, chunk })
  return request<any>({
    url: '/file/upload/chunk',
    method: 'POST',
    // 去掉重复的 module 传递，避免后端出现 "resume,resume"；fileId 与 chunkIndex 仅保留一份也可行
    // Spring 的 @RequestParam 可以从 multipart 表单字段解析这些键
    // 如果后端仍需在查询串读取，可改为 params: { fileId, chunkIndex } 或完全移除 params
    params: { fileId, chunkIndex },
    data: fd,
    onUploadProgress: onProgress,
  })
}

// 合并分片接口（通用文件）
export function mergeFileChunksApi(body: { fileId: string; totalChunks: number; module: string,fileName:string }) {
  console.log('mergeFileChunksApi body:', body)
  if (!body.fileId) throw new Error('fileId 不能为空')
  return request<any>({
    url: '/file/upload/merge',
    method: 'POST',
    data: body,
  })
}
